/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;

/**
 * This class controls the Menu screen for the GUI version of the intersection.
 * @author Luke
 */
public class Gui_Options {

    private JComboBox numHStreet;
    private JComboBox numVStreet;
    private JTextField probHCars = new JTextField("0.5");
    private JTextField probVCars = new JTextField("0.5");
    private JButton runButton = new JButton("Run Simulation");
    private JLabel option1 = new JLabel("# of H-Lanes: ");
    private JLabel option2 = new JLabel("# of V-Lanes: ");
    private JLabel option3 = new JLabel("Prob of H Cars: ");
    private JLabel option4 = new JLabel("Prob of V Cars: ");
    private Font f = new Font("Serif", Font.PLAIN, 36);
    private JPanel canvas = new JPanel(); //sets empty reference for later assignment
    private ArrayList<Number> streetValues = new ArrayList();
    private JFrame frame = new JFrame();

    /**
     *  Constructor for the class. Creates all the widgets in the GUI.
     * @param aPanel The JPanel from the previous menu screen
     * @param frame The frame from the previous menu screen
     */
    public Gui_Options(JPanel aPanel, JFrame frame) {
        this.frame = frame; //Set the varibale of frame to the existing frame
        JPanel optionBox = new JPanel(); //make empty reference to JPanel for later assignment
        canvas = aPanel; //assign canvas to passed in value

        Component[] tempCompList = canvas.getComponents(); //make a temporary array of components
        for (Component comp : tempCompList) { //for wach component in the list
            if (comp.getName().toString().equals("middle")) { //if it is the middle
                optionBox = ((JPanel) comp); //then assign option box to its value
            }
        }

        ButtonHandler btnHandler = new ButtonHandler(); //makes an instance of the btn handler class
        runButton.addActionListener(btnHandler); //sets the action listener ti the btn handler class

        setupNStreets(); //Makes the JComboBoxes for the number of streets
        setupLabels(); //Makes Labels for the combo boxes
        optionBox.setLayout(new GridLayout(0, 4)); //Set the grid layout of the JComboBox

        //row 1
        optionBox.add(makePadding()); //Makes padding for the options JPanel
        optionBox.add(option1); //Adds component to JPanel for the first JCombobox label
        optionBox.add(numHStreet); //Adds Combobox to the JPanel
        optionBox.add(makePadding()); //Add more padding to the JPanel

        //row 2
        optionBox.add(makePadding());
        optionBox.add(option2);
        optionBox.add(numVStreet);
        optionBox.add(makePadding());

        //row 3
        optionBox.add(makePadding());
        optionBox.add(option3);
        optionBox.add(probHCars);
        optionBox.add(makePadding());

        //row 4
        optionBox.add(makePadding());
        optionBox.add(option4);
        optionBox.add(probVCars);
        optionBox.add(makePadding());

        //row 5
        optionBox.add(makePadding());
        optionBox.add(makePadding());
        optionBox.add(runButton); //Add a run button

    }

    /**
     * Method to create the JComboBoxes for the GUI menu.
     */
    public void setupNStreets() {
        ListCellRenderer renderer = new DefaultListCellRenderer(); //make the default render instance
        ((JLabel) renderer).setHorizontalAlignment(SwingConstants.CENTER); //sets the default alignment to centre

        String[] hStreetString = {"1", "2", "3"}; //make the combo box options
        String[] vStreetString = {"1", "2", "3", "4"}; //as above

        numHStreet = new JComboBox(hStreetString); //make combobox objects have references
        numVStreet = new JComboBox(vStreetString); //as above

        numHStreet.setRenderer(renderer); //set the renderer for the combo boxes to new default
        numVStreet.setRenderer(renderer); //as above

        numHStreet.setSelectedIndex(1); //Set the default selection to "2" streets
        numVStreet.setSelectedIndex(1); //As above

        numHStreet.setFont(f); //Set the font of the text in the JCombobox
        numVStreet.setFont(f); //As above
    }

    /**
     * Modifies the attributes of the labels and JTextfields in this part of the GUI
     */
    public void setupLabels() {
        option1.setHorizontalAlignment(JLabel.RIGHT); //Set the alignment for the labels to right
        option2.setHorizontalAlignment(JLabel.RIGHT);
        option3.setHorizontalAlignment(JLabel.RIGHT);
        option4.setHorizontalAlignment(JLabel.RIGHT);

        option1.setFont(f); //Set the font of the labels
        option2.setFont(f);
        option3.setFont(f);
        option4.setFont(f);

        probHCars.setFont(f); //Sey he font in the JTextBoxes for car probability
        probVCars.setFont(f);
        probHCars.setHorizontalAlignment(JTextField.CENTER); //Center the text in teh JComboBoxs
        probVCars.setHorizontalAlignment(JTextField.CENTER);
    }

    /**
     * Makes padding for some of the objects that use the GridLayout
     * @return A blank JPanel that is able to be resized to the grid cell it is placed into.
     */
    public JPanel makePadding() { //Makes a blank JPanel
        JPanel tempPanel = new JPanel();
        return tempPanel;
    }

    /**
     * Make dialog box to pop up, like an error message if the wrong input is given into the JTextFields
     * @param message A warning message that the input into the JTextfield is unaccpetable.
     */
    public void makeDialog(Object message) { //Make the error warning dialog
        JOptionPane badInput = new JOptionPane(message, JOptionPane.ERROR_MESSAGE);
        message = message + "\nValue not between 0 and 1";
        badInput.showMessageDialog(canvas, message);
    }

    private class ButtonHandler implements ActionListener { //Handler for the inputs places intot eh JTextfields and JComboboxes

        public void actionPerformed(ActionEvent event) { //Method for button click
            int goodInputCheck = 0; //Counter to see if all inputs are good
            streetValues.clear(); //Resets the values for intersection
            streetValues.add(Integer.parseInt(numHStreet.getSelectedItem().toString())); //get combo value and auto box it a number
            streetValues.add(Integer.parseInt(numVStreet.getSelectedItem().toString())); //get combo value and auto box it a number

            try {
                float flt_userInput = Float.valueOf(probHCars.getText().toString().trim()).floatValue(); //Check if the input can become a float
                if ((flt_userInput > 0 || flt_userInput == 0) && (flt_userInput < 1 || flt_userInput == 1)) {
                    goodInputCheck++; //if yes inplments the goodInput counter
                    streetValues.add(flt_userInput); //autobox float to number and insert into arrayList
                } else {
                    makeDialog(""); //The input is bad and the error dialog is called
                }
            } catch (Exception e) {
                makeDialog(e); //THe input is bad and the error dialog is called and the error relevant displayed
            }

            try {
                float flt_userInput = Float.valueOf(probVCars.getText().toString().trim()).floatValue(); //Ass above except for other combox thsi time
                if ((flt_userInput > 0 || flt_userInput == 0) && (flt_userInput < 1 || flt_userInput == 1)) {
                    goodInputCheck++;
                    streetValues.add(flt_userInput); //autobox float to number and insert into arrayList
                } else {
                    makeDialog("");
                }
            } catch (Exception e) {
                makeDialog(e);
            }
            if (goodInputCheck == 2) { //Both inputs were good
                Gui_Intersection guiIntersection = new Gui_Intersection(streetValues, frame); //Make the guiIntersection
                guiIntersection.resetCanvas(canvas); //Reset the canvas, delete all widgets and inner componenets
                guiIntersection.initialise(); //Initialise the intersection to run
            }
        }
    }
}
